<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>2 普通回归和时间序列序列 | 编程语言笔记</title>
  <meta name="description" content="2 普通回归和时间序列序列 | 编程语言笔记" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="2 普通回归和时间序列序列 | 编程语言笔记" />
  <meta property="og:type" content="book" />
  
  
  
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="2 普通回归和时间序列序列 | 编程语言笔记" />
  
  
  

<meta name="author" content="云腾足下" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="Maple.html"/>
<link rel="next" href="DataProcess.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  jax: ["input/TeX","output/SVG"],
  extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"],
  TeX: {
    extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
  }
});
</script>
<script type="text/javascript"
   src="../../../MathJax/MathJax.js">
</script>


<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>序</a></li>
<li class="chapter" data-level="1" data-path="Maple.html"><a href="Maple.html"><i class="fa fa-check"></i><b>1</b> Maple</a><ul>
<li class="chapter" data-level="1.1" data-path="Maple.html"><a href="Maple.html#一句话tips"><i class="fa fa-check"></i><b>1.1</b> 一句话Tips</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="TS.html"><a href="TS.html"><i class="fa fa-check"></i><b>2</b> 普通回归和时间序列序列</a><ul>
<li class="chapter" data-level="2.1" data-path="TS.html"><a href="TS.html#一句话tips-1"><i class="fa fa-check"></i><b>2.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="2.2" data-path="TS.html"><a href="TS.html#gmm估计"><i class="fa fa-check"></i><b>2.2</b> GMM估计</a></li>
<li class="chapter" data-level="2.3" data-path="TS.html"><a href="TS.html#季节调整"><i class="fa fa-check"></i><b>2.3</b> 季节调整</a><ul>
<li class="chapter" data-level="2.3.1" data-path="TS.html"><a href="TS.html#r中有x12包可以做季节处理"><i class="fa fa-check"></i><b>2.3.1</b> R中有<code>x12</code>包可以做季节处理</a></li>
<li class="chapter" data-level="2.3.2" data-path="TS.html"><a href="TS.html#seasonal包有x13处理更加便捷"><i class="fa fa-check"></i><b>2.3.2</b> <code>seasonal</code>包有x13处理，更加便捷</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="DataProcess.html"><a href="DataProcess.html"><i class="fa fa-check"></i><b>3</b> 数据处理</a><ul>
<li class="chapter" data-level="3.1" data-path="DataProcess.html"><a href="DataProcess.html#一句话tips-2"><i class="fa fa-check"></i><b>3.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="3.2" data-path="DataProcess.html"><a href="DataProcess.html#rjsdmx包下载世界各大数据库数据"><i class="fa fa-check"></i><b>3.2</b> <code>RJSDMX</code>包下载世界各大数据库数据</a></li>
<li class="chapter" data-level="3.3" data-path="DataProcess.html"><a href="DataProcess.html#前向后向线性和样条插值"><i class="fa fa-check"></i><b>3.3</b> 前向、后向、线性和样条插值</a><ul>
<li class="chapter" data-level="3.3.1" data-path="DataProcess.html"><a href="DataProcess.html#signal包"><i class="fa fa-check"></i><b>3.3.1</b> <code>signal</code>包</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="统计.html"><a href="统计.html"><i class="fa fa-check"></i><b>4</b> 统计</a><ul>
<li class="chapter" data-level="4.1" data-path="统计.html"><a href="统计.html#一句话tips-3"><i class="fa fa-check"></i><b>4.1</b> 一句话Tips</a></li>
<li class="chapter" data-level="4.2" data-path="统计.html"><a href="统计.html#mcmc算法"><i class="fa fa-check"></i><b>4.2</b> MCMC算法</a><ul>
<li class="chapter" data-level="4.2.1" data-path="统计.html"><a href="统计.html#吉布斯抽样原理"><i class="fa fa-check"></i><b>4.2.1</b> 吉布斯抽样原理</a></li>
<li class="chapter" data-level="4.2.2" data-path="统计.html"><a href="统计.html#一些共轭先验分布的结论"><i class="fa fa-check"></i><b>4.2.2</b> 一些共轭先验分布的结论</a></li>
<li class="chapter" data-level="4.2.3" data-path="统计.html"><a href="统计.html#一个吉布斯抽样的典型案例"><i class="fa fa-check"></i><b>4.2.3</b> 一个吉布斯抽样的典型案例</a></li>
<li class="chapter" data-level="4.2.4" data-path="统计.html"><a href="统计.html#metropolis-和-m-h算法"><i class="fa fa-check"></i><b>4.2.4</b> Metropolis 和 M-H算法</a></li>
<li class="chapter" data-level="4.2.5" data-path="统计.html"><a href="统计.html#一些带贝叶斯估计的r包使用报告"><i class="fa fa-check"></i><b>4.2.5</b> 一些带贝叶斯估计的R包使用报告</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">编程语言笔记</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="TS" class="section level1">
<h1><span class="header-section-number">2</span> 普通回归和时间序列序列</h1>
<div id="一句话tips-1" class="section level2">
<h2><span class="header-section-number">2.1</span> 一句话Tips</h2>
<ul>
<li><code>gdpc</code>计算广义动态主成分。</li>
<li><code>POET::POETKhat</code>提供了计算Bai and NG (2002)因子数目的函数。</li>
<li><code>confint</code>函数返回系数的置信区间</li>
<li><code>bssm</code>拟合非线性卡尔曼滤波的包。<code>pomp</code>，<code>KFAS</code>也是。<code>pomp</code>好像接口更简单些，第四节有一个非线性的例子。</li>
<li><code>NlinTS</code>一个利用神经网络的格兰杰因果非线性检验。</li>
<li><code>slider</code>: 在任何R数据类型上提供类型稳定的滚动窗口函数，并支持累积窗口和扩展窗口。</li>
<li><code>testcorr</code>: 提供计算单变量时间序列中自相关显著性、双变量时间序列中互相关显著性、多变量序列中皮尔逊相关显著性和单变量序列i.i.d.特性的测试统计量的功能。<br />
</li>
<li><code>apt</code>一个阈值协整包。</li>
<li><code>fDMA</code>动态平均模型。卡尔曼滤波的贝叶斯模型平均。</li>
<li><code>MuMIn</code>利用信息准则进行模型平均的包。</li>
<li><code>MSBVAR</code>提供了贝叶斯框架下的马尔科夫转移VAR。<code>MSwM</code>是一个单方程（非单变量）的马尔科夫转移模型估计。</li>
<li>因子变虚拟变量：<code>model.matrix</code>可以生成回归所需要的矩阵，可以把因子变量变成虚拟变量。</li>
<li><code>mfGARCH</code>包估计混频GARCH。</li>
<li><code>TED::ur.za.fast</code>和<code>urca::ur.za</code>未知断点的单位根检验。</li>
<li><code>mFilter</code>包有各种经济和金融常用的滤波，如HP，BK等滤波（好像没有更新了，官网包的镜像没有找到）。但是可以使用<code>FRAPO</code>包的<code>trdhp</code>函数来计算HP滤波 。</li>
<li><code>svars</code>是一个数据驱动的结构VAR包。<code>vars</code>是一个VAR各种估计和诊断的标准包。<code>tsDyn</code>也有线性VAR和VECM的估计，其中它还允许包含外生变量。</li>
<li><code>lmtest</code>有<code>grangertest()</code>做双变量格兰杰因果检验。<code>MTS::GrangerTest(regdata[,-c(1,2)],2,locInput = 1)</code>也可以，而且可以做多个变量是不是某个变量的格兰杰原因。<code>locInput</code>表示因变量是第几列。</li>
<li><code>stats4</code>包提供了函数<code>mle</code>可以进行极大似然估计，还可以固定部分参数，优化其他参数，这其实是集中似然的思想。关键是它还返回方差协方差矩阵。语法如下，</li>
</ul>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="TS.html#cb2-1"></a><span class="kw">mle</span>(minuslogl, <span class="dt">start =</span> <span class="kw">formals</span>(minuslogl), <span class="dt">method =</span> <span class="st">&quot;BFGS&quot;</span>,</span>
<span id="cb2-2"><a href="TS.html#cb2-2"></a>    <span class="dt">fixed =</span> <span class="kw">list</span>(), nobs, ...) <span class="co"># 注意它的初值是一个list</span></span></code></pre></div>
<ul>
<li><code>dynlm::dynlm</code>包一个比<code>lm</code>更强大线性回归结构，优点有三：
<ul>
<li>可以使用差分、滞后等表述，如<code>d(y)~L(y,2)</code>，可以直接添加趋势项<code>trend(y)</code>将使用<code>$(1:n)/Freq$</code>作为回归元。</li>
<li>可以进行工具变量估计。
但要注意，他的数据不是数据框，而是一个<code>ts</code>对象。</li>
</ul></li>
<li><code>nardl</code>估计非线性协整分布滞后模型。</li>
<li><code>rugarch</code>：单变量garch建模。一个比<code>forcast</code>更好用的时序建模包。可以用<code>show</code>函数来返回一个丰富的结果，包括一些检验结果。</li>
<li><code>rmgarch</code>：多变量garch建模。包括dcc,adcc,gdcc等。</li>
<li><code>stats</code>包中的<code>ARMAtoMA</code>函数可以计算AR变成MA。<code>vars</code>包的<code>Phi</code>返回VAR的移动平均系数。</li>
<li><code>vars</code>包里面的<code>Phi</code>函数可以把VAR变成VMA。使用<code>summary</code>函数来摘要var的估计结果，会给粗特征根，残差相关矩阵等。</li>
<li><code>tsDyn</code>包的<code>VECM</code>函数比较好用，可以包括外生变量，可以选择OLS或Joson方法。这个包也是可以估计线性VAR的，主要是他的<code>lineVar</code>函数。<code>egcm</code>包是恩格尔格兰杰协整检验，这个检验在<code>urca</code>包里业可行。</li>
<li><code>TSA::periodogram</code>可以做谱分解。</li>
<li><code>bvarsv</code>时变参数var建模</li>
<li><code>nls</code>非线性最小二乘法函数</li>
<li><code>highfrequance</code>里面有不少意思的函数，包括<code>HAR</code>。
## 回归中关于公式的理解和构造</li>
</ul>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="TS.html#cb3-1"></a><span class="co"># 构造公式, 只要包含波浪线就意味着这是一个公式。</span></span>
<span id="cb3-2"><a href="TS.html#cb3-2"></a>F1 &lt;-<span class="st"> </span>dist <span class="op">~</span><span class="st"> </span>speed <span class="op">-</span><span class="st"> </span><span class="dv">1</span></span>
<span id="cb3-3"><a href="TS.html#cb3-3"></a><span class="co"># 获得公式中所有的变量</span></span>
<span id="cb3-4"><a href="TS.html#cb3-4"></a>mf1 &lt;-<span class="st"> </span><span class="kw">model.frame</span>(F1,<span class="dt">data =</span> cars)</span>
<span id="cb3-5"><a href="TS.html#cb3-5"></a><span class="co"># 抽取因变量</span></span>
<span id="cb3-6"><a href="TS.html#cb3-6"></a><span class="kw">model.response</span>(mf1)</span>
<span id="cb3-7"><a href="TS.html#cb3-7"></a><span class="co"># 抽取自变量</span></span>
<span id="cb3-8"><a href="TS.html#cb3-8"></a><span class="kw">model.matrix</span>(F1, <span class="dt">data =</span> cars)</span></code></pre></div>
<p>公式的高级应用还有一个包<code>Formula</code>，其说明文件很到位。主要阐述了<code>|</code>的使用方式。</p>
</div>
<div id="gmm估计" class="section level2">
<h2><span class="header-section-number">2.2</span> GMM估计</h2>
<p><span class="math display">\[
i _t =\beta_0 + \beta_1pi_t + \beta_2GDP_t + \beta_3hp_t + \beta_4i_{t-1} + \varepsilon_t
\]</span></p>
<p>因包含因变量的滞后项从而有内生性，欲使用<span class="math inline">\(i_{t-2},i_{t-3},i_{t-4}\)</span>作为工具变量，从而做一个GMM估计，亦即整个方程的矩条件为，</p>
<p><span class="math display">\[\begin{align}
E(pi_t\varepsilon_t) = 0\\

E(GDP_t\varepsilon_t) = 0\\

E(hp_t\varepsilon_t) = 0\\

E(i_{t-2}\varepsilon_t) = 0\\

E(i_{t-3}\varepsilon_t) = 0\\

E(i_{t-4}\varepsilon_t) = 0
\end{align}\]</span></p>
<p>利用这些矩条件的GMM估计在<code>gmm</code>包中的写法为，</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="TS.html#cb4-1"></a>gmmrlt &lt;-<span class="st"> </span><span class="kw">gmm</span>(<span class="dt">g =</span> it <span class="op">~</span><span class="st"> </span>pi <span class="op">+</span><span class="st"> </span>gdp <span class="op">+</span><span class="st"> </span>hp <span class="op">+</span><span class="st"> </span>it1, <span class="dt">x =</span> <span class="op">~</span><span class="st"> </span>pi <span class="op">+</span><span class="st"> </span>gdp <span class="op">+</span><span class="st"> </span>hp <span class="op">+</span><span class="st"> </span>it2 <span class="op">+</span><span class="st"> </span>it3 <span class="op">+</span><span class="st"> </span>it4,<span class="dt">data =</span> dwg0)</span>
<span id="cb4-2"><a href="TS.html#cb4-2"></a><span class="kw">summary</span>(gmmrlt)</span></code></pre></div>
<p>其中<code>g</code>可直接写成公式，<code>x</code>即为工具变量集。</p>
</div>
<div id="季节调整" class="section level2">
<h2><span class="header-section-number">2.3</span> 季节调整</h2>
<div id="r中有x12包可以做季节处理" class="section level3">
<h3><span class="header-section-number">2.3.1</span> R中有<code>x12</code>包可以做季节处理</h3>
<p>注意：</p>
<ul>
<li>要先下载美国统计局的x12程序包，并在调用函数时，记得写上所以存储的路径。</li>
<li>仅可处理R中内置的时间序列对象<code>ts</code>。</li>
<li>示例代码：</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="TS.html#cb5-1"></a><span class="kw">library</span>(x12)</span>
<span id="cb5-2"><a href="TS.html#cb5-2"></a><span class="kw">data</span>(AirPassengers)</span>
<span id="cb5-3"><a href="TS.html#cb5-3"></a>x12out &lt;-<span class="st"> </span><span class="kw">x12work</span>(AirPassengers,</span>
<span id="cb5-4"><a href="TS.html#cb5-4"></a>             <span class="dt">x12path =</span> <span class="st">&#39;C:</span><span class="ch">\\</span><span class="st">ado</span><span class="ch">\\</span><span class="st">plus</span><span class="ch">\\</span><span class="st">WinX12</span><span class="ch">\\</span><span class="st">x12a</span><span class="ch">\\</span><span class="st">x12a.exe&#39;</span>,<span class="dt">keep_x12out =</span> <span class="ot">FALSE</span>)</span>
<span id="cb5-5"><a href="TS.html#cb5-5"></a>x12out<span class="op">$</span>d11 <span class="co">#此即为调整后的时间序列</span></span></code></pre></div>
<p>其中，<code>keep_x12out</code>参数表示是否要保留计算后的文件。</p>
</div>
<div id="seasonal包有x13处理更加便捷" class="section level3">
<h3><span class="header-section-number">2.3.2</span> <code>seasonal</code>包有x13处理，更加便捷</h3>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="TS.html#cb6-1"></a><span class="kw">library</span>(seasonal)</span>
<span id="cb6-2"><a href="TS.html#cb6-2"></a>m &lt;-<span class="st"> </span><span class="kw">seas</span>(AirPassengers) <span class="co"># x13 处理, AirPassengers是一个ts对象</span></span>
<span id="cb6-3"><a href="TS.html#cb6-3"></a><span class="kw">final</span>(m) <span class="co"># 最终调整序列</span></span>
<span id="cb6-4"><a href="TS.html#cb6-4"></a><span class="kw">plot</span>(m) <span class="co"># 绘制调整和未调整序列</span></span></code></pre></div>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="Maple.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="DataProcess.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["CBook.pdf"],
"toc": {
"collapse": "subsection"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
